//
//  ILDetailView.m
//  ILife
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILDetailView.h"
#import "ILDailySelectModel.h"
#import <UIImageView+WebCache.h>

@implementation ILDetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }else{
        
        
        //背景图
        self.backgroudImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
         self.backgroudImg.userInteractionEnabled = YES;
        //主题图
        self.coverImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT/2-20)];
         self.coverImg.userInteractionEnabled = YES;


        
        //主题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 54+SCREEN_HEIGHT/2, SCREEN_WIDTH, 30)];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.textColor = [UIColor whiteColor];
        //类型 ---时长
        self.category_durLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 94+SCREEN_HEIGHT/2, SCREEN_WIDTH, 20)];
        self.category_durLabel.font = [UIFont systemFontOfSize:13];
        self.category_durLabel.textColor = [UIColor whiteColor];
        
        //描述详情
        self.descLabel = [[UILabel alloc]init];
       
        
        self.icoPlay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"play.png"]];
        _icoPlay.frame = CGRectMake(SCREEN_WIDTH/2-40, SCREEN_HEIGHT/3-30, 60, 60);
        
        
        //分割线
        self.lineView= [[UIView alloc]initWithFrame:CGRectMake(10, 84+SCREEN_HEIGHT/2, arc4random()%(int)SCREEN_WIDTH, 1)];
        _lineView.backgroundColor = [UIColor grayColor];
        
        

       
        [self  addSubview:self.backgroudImg];
        [self.backgroudImg addSubview:_descLabel];
        [self.backgroudImg addSubview:_coverImg];
        [self.backgroudImg addSubview:_titleLabel];
        [self.backgroudImg addSubview:_category_durLabel];
        [self.backgroudImg addSubview:_lineView];
        [self.backgroudImg addSubview:_icoPlay];
        
    }
    return self;
}



-(void)configureWithData:(ILDailySelectModel*)model
{
    
    
    if (model) {
       
    
        
        [self.backgroudImg sd_setImageWithURL:[NSURL URLWithString:model.coverBlurred]placeholderImage:[UIImage imageNamed:@"ilife"]];
        
//        self.coverImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.coverForDetail]]];
        
        [self.coverImg sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail] placeholderImage:[UIImage imageNamed:@"ilife"]];
        

        CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        animation.duration = 0.5;
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        animation.values = values;
        [self.coverImg.layer addAnimation:animation forKey:nil];
        
        self.titleLabel.text = model.title;
        
        NSString *str = [NSString stringWithFormat:@"/%ld'%ld''",model.duration/60,model.duration%60];
        self.category_durLabel.text = [[NSString stringWithFormat:@"#%@",model.category]stringByAppendingString:str];
        
        
        if ([self heightForDesc:model.des]>SCREEN_HEIGHT-200-SCREEN_HEIGHT/2) {
            self.descLabel.frame = CGRectMake(20, 114+SCREEN_HEIGHT/2, SCREEN_WIDTH-40, SCREEN_HEIGHT-200-SCREEN_HEIGHT/2);
        }else{
            self.descLabel.frame = CGRectMake(20, 114+SCREEN_HEIGHT/2, SCREEN_WIDTH-40, [self heightForDesc:model.des]);
            
        }
        self.descLabel.font = [UIFont systemFontOfSize:12];
        self.descLabel.textColor = [UIColor whiteColor];
        self.descLabel.numberOfLines = 0;
        self.descLabel.text = model.des;
        
    }

}


//计算详情描述高度  自适应
-(CGFloat)heightForDesc:(NSString *)desc
{
    CGRect descBounds = [desc boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-40, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName] context:nil];
    
    return descBounds.size.height;
    
}









@end
